package com.example.springpethospital.service;

import com.example.springpethospital.entity.Const;
import com.example.springpethospital.entity.Illcase;
import com.example.springpethospital.mapper.IllcaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IllcaseServiceImpl implements IllcaseService {
    @Autowired
    IllcaseMapper illcaseMapper;
    @Autowired
    IllcaseFileService illcaseFileService;

    @Override
    public boolean AddCase(Illcase illcase) {
        return illcaseMapper.AddCase(illcase.name, illcase.reception, illcase.examination, illcase.diagnostic, illcase.treatment);
    }

    @Override
    public boolean ModifyCase(Illcase illcase) {
        return illcaseMapper.ModifyCase(illcase.id, illcase.name, illcase.reception, illcase.examination, illcase.diagnostic, illcase.treatment);
    }

    @Override
    public boolean RemoveCase(int id) {
        int cnt = illcaseFileService.DeleteFileByCase(id);
        System.out.println("删除病例" + id + "的" + cnt + "个文件");
        return illcaseMapper.RemoveCase(id);
    }

    @Override
    public List<Illcase> GetCases(String name, Integer paging) {
        if (paging == null) {
            return illcaseMapper.GetCases(name);
        } else {
            int offset = paging * Const.NUM_PER_PAGE;
            int limit = Const.NUM_PER_PAGE;
            return illcaseMapper.GetCasesWithPage(name, offset, limit);
        }
    }

    @Override
    public int getTotalPages(String name) {
        return (int) Math.ceil((double) illcaseMapper.getCaseNum(name) / (double) Const.NUM_PER_PAGE);
    }
}